<?php
namespace app\common\model;
use app\service\MsgService;
use app\common\model\User as UserModel;

class Grade extends Base
{
    protected function initialize()
    {
        parent::initialize();
    }

    /* grade_id 转 grade_name */
    public static function id2name ($id) {
        return self::where('id',$id)->value('grade_name');
    }

    /* 改变等级 */
    public static function changeGrade ($uid,$old,$new,$note='system') {
        $res = User::where('id',$uid)->update([
            'grade_id' => $new
        ]);
        $userInfo = UserModel::getOne(['id'=>$uid],'mall_id');
        if($res) {
            //记录变更log
            ActionLog::create([
                'uid' => $uid,
                'type' => 1,
                'note' => $note,
                'old' => $old,
                'new' => $new
            ]);
            if($note=='system') {
                Daily::inc('grade'.$new.'_add');
            }
            //发送等级变更提醒
            MsgService::S([
                'mall_id' => $userInfo['mall_id']
            ])->sendTemp('DENG_JI',$userInfo['mall_id'],$uid,[
                'first' => '您的等级已变更',
                'now_grade' => self::id2name($old),
                'prv_grade' => self::id2name($new),
                'remark' => ''
            ],$url='');
        }
    }
}